import { ScrollView } from 'cc';
import { _decorator, Component, Node } from 'cc';
import ScrollHelper from './ScrollHelper';
import AbstractComponent from '../base/AbstractComponent';
import { find } from 'cc';
import { Label } from 'cc';
import UtilTool from '../tools/UtilTool';
import CommConst from '../const/CommConst';
import wsTool from '../net/ws';
const { ccclass, property } = _decorator;

let PAGE_DEFAULT_SIZE = 30

/**
 * 中奖记录
 */
@ccclass('LotteryRecord')
export class LotteryRecord extends AbstractComponent {

    @property(ScrollView)
    scrollView : ScrollView = null;

    @property(Node)
    contentNode : Node = null;

    @property(ScrollHelper)
    scrollHelper : ScrollHelper = null;

    @property(Node)
    emptyNode : Node = null;

    private itemNum = 9
    // [listData]列表数据  [isLoading]是否加载中 [loadOver]是否加载结束(如果没有新的数据则认为是加载结束) [currPage]当前已经请求的第几页的数据 [dataDirty]是否需要重新拉取数据 
    // 记录数据
    private configData = {listData : [], isLoading : false, loadOver : false, currPage : 0, dataDirty : true, pageSize : PAGE_DEFAULT_SIZE}

    start() {
        
        this.reqData(true)

        this.emptyNode.active = false

    }

    // 发送测试数据
    sendGetTestData(page, pageSize) : Promise<any> {

        return new Promise((resolve, reject) => {

            let data = {
                list : []
            }

            if(page >= 3) {
                resolve(data) // 大于等于3页就不再请求数据（即一共只有30个）
                return
            }

            for(let i = 0; i < pageSize; i++) {
                let index = page * pageSize + i + 1
                let item = {
                    award : 2000,
                    date :120102100,
                }
                data.list.push(item)
            }

            resolve(data)

        })

    }

    // 默认为当前页的下一页
    reqData(isInit=true) {

        this.configData.isLoading = true

        wsTool.send({"msgCode":CommConst.Req_REWARD_RECORD, "page":this.configData.currPage, "pageSize":this.configData.pageSize})
        // this.sendGetTestData(this.configData.currPage, PAGE_DEFAULT_SIZE) // 测试数据
        .then((data) => {
            UtilTool.printLog("test multi record list", data);
            UtilTool.printLog("test reqData", data)

            this.configData.currPage += 1
            this.configData.isLoading = false
            
            data.list = data.lotteryIncomeList

            data.list.forEach(element => {
                this.configData.listData.push(element)
            });
            if (data.list==null || data.list.length < this.configData.pageSize) {
                this.configData.loadOver = true
            }
    
            if (data.list.length>0) {
    
                let param = {
                    init:isInit, 
                    data:this.configData.listData,
                    itemNum:this.itemNum,
                    itemFun: (obj,idx,objIdx)=>{
                        this.refreshItem(obj,idx,objIdx)
                    },
                    endFun:()=>{
                        this.OnScrollEnd()
                    }
                }
    
                this.scrollHelper.setData(param)
            }

            this.emptyNode.active = this.configData.listData.length <= 0
        })
    }

    // 刷新item
    refreshItem(obj,idx,objIdx) {
        
        if (this.configData.listData.length <= 0) {
            return
        }

        // let data = {
        //     createTime: 1692413271000, gold: 5, humanId: 200003, id: 5
        // }

        let data = this.configData.listData[idx]
        find('label_award', obj).getComponent(Label).string = UtilTool.formatStr("+%s USDT", data.gold) // 奖励

        let date = UtilTool.getTimeData(data.createTime)
        let dateStr = UtilTool.formatStr("%s/%s/%s %s:%s", date.year, date.month, date.day, date.hour, date.min) 
        find('label_date', obj).getComponent(Label).string = dateStr  // 日期

    }

    // 滚动视图滑到底部回调
    OnScrollEnd() {

        if (this.configData.isLoading) {
            return
        }
        if (this.configData.loadOver) {
            return
        }

        this.reqData(false)
    }

}

